<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) https://inphp.cc All rights reserved.
// | 该文件源码由INPHP官方提供，使用协议以INPHP官方公告为准。
// +----------------------------------------------------------------------
// | 分类单页/列表页
// +----------------------------------------------------------------------
namespace app\cms\http\web;

use app\cms\tags\archiveList;
use Inphp\Core\Context;

class category extends controller
{
    public function index(string|int|null $id)
    {
        //设置当前展示页面类型
        Context::set("CMS_PAGE", "category");
        $id = $id ?? GET("id");
        $category = \app\cms\tags\category::process([
            "id"    => $id
        ]);
        if (empty($category)) {
            $this->show404();
            return;
        }
        if (!empty($category["keyword"])) {
            viewAssign("keyword", $category["keyword"]);
        }
        if (!empty($category["description"])) {
            viewAssign("description", $category["description"]);
        }
        viewAssign("CMS_PAGE_TITLE", $category["name"]);
        viewAssign("category", $category);
        //列表、分页
        $page = GET("page", 1);
        $page = is_numeric($page) && $page > 0 ? ceil($page) : 1;
        $total = GET("total", 30);
        $total = is_numeric($total) && $total > 0 ? ceil($total) : 30;
        $total = min($total, 100);
        $list = archiveList::process([
            "categoryId"    => $category["id"],
            "page"          => $page,
            "total"         => $total,
            "pager"         => true
        ]);
        viewAssign("list", $list);
        //处理模板
        $tpl = !empty($category["template"]) ? $category["template"] : null;
        //设置模板
        $this->setTPL($tpl);
    }

    public function __call(string $name, array $arguments)
    {
        // TODO: Implement __call() method.
        $name = stripos($name, ".html") > 0 ? substr($name, 0, stripos($name, ".html")) : $name;
        $name = is_numeric($name) ? ceil($name) : $name;
        $this->index($name);
    }
}